package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.app.controller.history.BrowsingHistoryController;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.AttrValueService;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestHeader;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private BrowsingHistoryService browsingHistoryService;

    @Override
    public RoomDetailVo getRoomDetailById(Long id, Long userId) {
        RoomInfo roomInfo = getById(id);
        if (roomInfo == null){return null;}

        ApartmentItemVo apartmentItemVo = apartmentInfoMapper.selectApartment(roomInfo.getApartmentId());
        apartmentItemVo.setLabelInfoList(labelInfoMapper.selectLabelById(roomInfo.getApartmentId()));
        List<GraphInfo> graphInfos = graphInfoMapper.selectListByItemTypeById(ItemType.APARTMENT, roomInfo.getApartmentId());
        apartmentItemVo.setGraphVoList(graphInfos);
        apartmentItemVo.setMinRent(roomInfoMapper.selectMinRentByApartmentId(roomInfo.getApartmentId()));

        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, id);
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectArrtById(id);
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityById(id);
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelById(id);
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectPayById(id);
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectFeeById(id);
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectLeaseTermById(id);

        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setIsDelete(roomInfo.getIsDeleted()==1);
        roomDetailVo.setIsCheckIn(roomInfo.getIsRelease() == ReleaseStatus.RELEASED);

        //触发历史记录保存
        browsingHistoryService.saveHistory(userId,id);

        return roomDetailVo;
    }

    @Override
    public void pageRoomItem(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.pageRoomItem(page,queryVo);
    }

    @Override
    public void pageItemByApartmentId(Page<RoomItemVo> page, Long id) {
        roomInfoMapper.pageItemByApartmentId(page,id);
    }


}




